package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.GoodsCommissionDTO;
import top.zywork.dto.PagerDTO;
import top.zywork.query.GoodsCommissionQuery;
import top.zywork.service.GoodsCommissionService;
import top.zywork.vo.GoodsCommissionVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;

import java.util.List;

/**
 * GoodsCommissionController控制器类<br/>
 *
 * 创建于2019-07-22<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/goods-commission")
@Slf4j
public class GoodsCommissionController extends BaseController {

    private static final String SAVE_FROM_GOODS = "goods";
    private static final String SAVE_FROM_GOODS_SKU = "goodsSku";

    private GoodsCommissionService goodsCommissionService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GoodsCommissionVO")
    public ResponseStatusVO save(@RequestBody @Validated GoodsCommissionVO goodsCommissionVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCommissionService.save(BeanUtils.copy(goodsCommissionVO, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GoodsCommissionVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GoodsCommissionVO> goodsCommissionVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCommissionService.saveBatch(BeanUtils.copyListObj(goodsCommissionVOList, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @PostMapping("admin/remove-and-batch-save/{goodsOrSku}/{id}")
    @SysLog(description = "后台删除并重新保存商品SKU分销比例")
    public ResponseStatusVO removeAndSaveBatch(@PathVariable("goodsOrSku") String goodsOrSku, @PathVariable("id") Long id,
                                               @RequestBody @Validated List<GoodsCommissionVO> goodsCommissionVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        Long goodsId = null;
        Long goodsSkuId = null;
        if (SAVE_FROM_GOODS.equals(goodsOrSku)) {
            goodsId = id;
        } else if (SAVE_FROM_GOODS_SKU.equals(goodsOrSku)) {
            goodsSkuId = id;
        }
        goodsCommissionService.removeAndSaveBatch(goodsId, goodsSkuId, BeanUtils.copyListObj(goodsCommissionVOList, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GoodsCommissionVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        goodsCommissionService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GoodsCommissionVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        goodsCommissionService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GoodsCommissionVO")
    public ResponseStatusVO update(@RequestBody @Validated GoodsCommissionVO goodsCommissionVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = goodsCommissionService.update(BeanUtils.copy(goodsCommissionVO, GoodsCommissionDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GoodsCommissionVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GoodsCommissionVO> goodsCommissionVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCommissionService.updateBatch(BeanUtils.copyListObj(goodsCommissionVOList, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GoodsCommissionVO")
    public ResponseStatusVO active(@RequestBody GoodsCommissionVO goodsCommissionVO) {
        goodsCommissionService.update(BeanUtils.copy(goodsCommissionVO, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GoodsCommissionVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GoodsCommissionVO> goodsCommissionVOList) {
        goodsCommissionService.updateBatch(BeanUtils.copyListObj(goodsCommissionVOList, GoodsCommissionDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GoodsCommissionVO goodsCommissionVO = new GoodsCommissionVO();
        Object obj = goodsCommissionService.getById(id);
        if (obj != null) {
            goodsCommissionVO = BeanUtils.copy(obj, GoodsCommissionVO.class);
        }
        return ResponseStatusVO.ok("查询成功", goodsCommissionVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = goodsCommissionService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCommissionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GoodsCommissionQuery goodsCommissionQuery) {
        PagerDTO pagerDTO = goodsCommissionService.listAllByCondition(goodsCommissionQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCommissionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GoodsCommissionQuery goodsCommissionQuery) {
        PagerDTO pagerDTO = goodsCommissionService.listPageByCondition(goodsCommissionQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCommissionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @Autowired
    public void setGoodsCommissionService(GoodsCommissionService goodsCommissionService) {
        this.goodsCommissionService = goodsCommissionService;
    }
}
